package com.yld.car.market;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.lidroid.xutils.BitmapUtils;
import com.yld.car.common.ConstantUtils;
import com.yld.car.common.NetworkProgressUtils;
import com.yld.car.domain.DealerBaseInfo;
import com.yld.car.domain.UserInfo;
import com.yld.car.market.tab.TabActivityGroup;
import com.yld.car.view.CircularImage;

/**
 * 
 * 我的信息页面
 * 
 * @author apple
 * 
 */
public class MyInfoActivity extends BaseActivity implements OnItemClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.my);
		mSettings = this.getSharedPreferences("person_state", MODE_PRIVATE);
		mEditor = mSettings.edit();
		TextView titleText = (TextView) findViewById(R.id.title);
		// 设置标题

		titleText.setText(R.string.main_ucenter);

		list1 = (ListView) findViewById(R.id.list1);
		list1.setOnItemClickListener(this);
		layoutHeader = (RelativeLayout) findViewById(R.id.layout_header);
		layoutHeader.setOnClickListener(headerClickListener);
		utils = NetworkProgressUtils.getInstance();
		initTopViews();
		new MyInfoTask().execute("");
		datas = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> localHashMap1 = new HashMap<String, String>();
		localHashMap1.put("title", "car");
		localHashMap1.put("title_s", "我的车源");
		localHashMap1.put("content", "");
		datas.add(localHashMap1);
		HashMap<String, String> localHashMap2 = new HashMap<String, String>();
		localHashMap2.put("title", "addCar");
		localHashMap2.put("title_s", "添加新车辆");
		localHashMap2.put("content", "");
		datas.add(localHashMap2);

		// datas = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> localHashMap3 = new HashMap<String, String>();
		localHashMap3.put("title", "queryPayToCar");
		localHashMap3.put("title_s", "查看已购车辆");
		localHashMap3.put("content", "");
		datas.add(localHashMap3);
		HashMap<String, String> localHashMap4 = new HashMap<String, String>();
		localHashMap4.put("title", "myselfAttentionCar");
		localHashMap4.put("title_s", "我关注的车辆");
		localHashMap4.put("content", "");
		datas.add(localHashMap4);

		HashMap<String, String> localHashMap5 = new HashMap<String, String>();
		localHashMap5.put("title", "updatePwd");
		localHashMap5.put("title_s", "修改密码");
		localHashMap5.put("content", "");
		datas.add(localHashMap5);
		MyInfoAdapter adapter = new MyInfoAdapter(this, datas);
		list1.setAdapter(adapter);

		Button logout = (Button) findViewById(R.id.logoutButton);

		logout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				File f = MyInfoActivity.this.getFileStreamPath("userInfo");
				System.out.println(f.delete());

				Intent intent = new Intent();
				intent.setClass(getApplicationContext(),
						GuidePageActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				showDialogFinish("您确定退出吗？", intent);

			}
		});
	}

	private RelativeLayout layoutHeader;
	private View.OnClickListener headerClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			Intent intent = new Intent();
			intent.setClass(MyInfoActivity.this, MyBaseActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(intent);

		}
	};

	private void initTopViews() {
		detailBg = (ImageView) findViewById(R.id.detailBg);
		detailBg.setVisibility(View.VISIBLE);

	}

	private List<HashMap<String, String>> datas;
	private ListView list1;
	private ImageView detailBg;

	private class MyInfoAdapter extends BaseAdapter {

		private Context mContext;
		private LayoutInflater mLayoutInflater;
		private List<HashMap<String, String>> datas;

		// 个人中心图标id
		private int[] ids = { R.drawable.ucenter_my_car_market,
				R.drawable.ucenter_add_car, R.drawable.ucenter_pay_car,
				R.drawable.ucenter_my_attention_car,
				R.drawable.ucenter_update_pwd };

		public MyInfoAdapter(Context context,
				List<HashMap<String, String>> datas) {

			this.mContext = context;
			this.datas = datas;
			mLayoutInflater = LayoutInflater.from(mContext);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datas.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return datas.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			// String str = datas.get(position).get("title");
			// if ("car".equals(str)) {
			// convertView = mLayoutInflater.inflate(R.layout.my_items_car,
			// null);
			//
			// } else if ("base".equals(str)) {
			//
			// convertView = mLayoutInflater.inflate(R.layout.my_items, null);
			//
			// }
			if (convertView == null) {

				convertView = mLayoutInflater.inflate(R.layout.my_items_car,
						null);

			}

			ImageView image = (ImageView) convertView
					.findViewById(R.id.imageView1);
			image.setImageResource(ids[position]);
			//convertView.setMinimumHeight(150);
			TextView title = (TextView) convertView
					.findViewById(R.id.itemTitle);
			title.setText(datas.get(position).get("title_s"));
			return convertView;
		}

	}

	private SharedPreferences mSettings;
	private SharedPreferences.Editor mEditor;

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {

		// if (0 == position) {
		// Intent intent = new Intent();
		// intent.setClass(this, TabActivityGroup.class);
		// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// mEditor.putBoolean("flag", true).commit();
		// startActivity(intent);
		// finish();
		// } else if (1 == position) {
		//
		// Intent intent = new Intent();
		// intent.setClass(this, MyBaseActivity.class);
		// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// startActivity(intent);
		// }
		if (position == 0) {

			Intent intent = new Intent();
			intent.setClass(MyInfoActivity.this, MyCarMarketActivity.class);
			startActivity(intent);
		} else if (position == 1) {

			Intent intent = new Intent();
			intent.setClass(MyInfoActivity.this, MyCarPublishActivity.class);
			startActivity(intent);
		} else if (position == 2) {

			Intent intent = new Intent();
			intent.setClass(MyInfoActivity.this, CostHomePageActivity.class);

			startActivity(intent);
		} else if (position == 3) {

			Intent intent = new Intent();
			intent.setClass(MyInfoActivity.this, CostHomePageActivity.class);
			intent.putExtra("isAttention", true);
			startActivity(intent);
		} else if (position == 4) {

			Intent intent = new Intent();
			intent.setClass(MyInfoActivity.this, UpdatePasswordActivity.class);
			startActivity(intent);
		}

	}

	private NetworkProgressUtils utils;

	private class MyInfoTask extends AsyncTask<String, String, DealerBaseInfo> {

		@Override
		protected DealerBaseInfo doInBackground(String... params) {

			UserInfo u = (UserInfo) readObject("userInfo");
			HashMap<String, String> allParams = new HashMap<String, String>();
			allParams.put("dealerId", u.getUserId());

			Object o = utils.getWebservicesByData(
					ConstantUtils.NAME_SPACE,
					ConstantUtils.ALL_METHROD_NAME.get(25),
					ConstantUtils.NAME_SPACE
							+ ConstantUtils.ALL_METHROD_NAME.get(25),
					ConstantUtils.GET_DEALER_INFO_URL, allParams);
			// System.out.println(o);
			DealerBaseInfo d = utils.parseBaseDealerInfo(o.toString(), false);

			return d;

		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			
			ImageView imageView = (ImageView) findViewById(R.id.nextStep);
			imageView.setVisibility(View.GONE);
		}
		@Override
		protected void onPostExecute(DealerBaseInfo result) {

			if (result != null && (result instanceof DealerBaseInfo)) {

				if (result.getUserimg() != null) {
					String downloadUrl = "";
					if (result.getUserimg().indexOf("aodi") >= 0) {

						downloadUrl = ConstantUtils.DOWNLOAD_IMAGE_URL_NEW
								+ result.getUserimg();
					} else {
						downloadUrl = ConstantUtils.DOWNLOAD_IMAGE_URL
								+ result.getUserimg();
					}
					// BitmapUtils bitmapUtils = new BitmapUtils(
					// getApplicationContext());
					CircularImage imageView = (CircularImage) findViewById(R.id.detailAvatar);
					imageView.setVisibility(View.VISIBLE);
					AQuery aq = new AQuery(MyInfoActivity.this);
					aq.id(imageView).image(downloadUrl);
					// 加载网络图片
					// bitmapUtils.display(imageView, downloadUrl);

				}

				TextView name = (TextView) findViewById(R.id.detailName);
				name.setVisibility(View.VISIBLE);
				name.setText(result.getComppeople());
				TextView post = (TextView) findViewById(R.id.detailPost);
				post.setVisibility(View.VISIBLE);
				post.setText(result.getTelephone());

				TextView address = (TextView) findViewById(R.id.detailAddress);
				address.setVisibility(View.VISIBLE);
				address.setText(result.getAddress());
				ImageView imageView = (ImageView) findViewById(R.id.nextStep);
				imageView.setVisibility(View.VISIBLE);
			}
			super.onPostExecute(result);
		}

	}

}
